package devian.study;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.webkit.*;
import android.widget.*;

public class HybridApp extends Activity {
	WebView mWebView;
	TextView mTextView;
	EditText mEditText;
	Button mButton;
	String mStringHead;

	private final Handler handler = new Handler();
    
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.webview);
        mTextView = (TextView) findViewById(R.id.textview);
        mButton = (Button) findViewById(R.id.button);
        mEditText = (EditText) findViewById(R.id.EditText01);
        mStringHead = this.getString(R.string.string_head);
        
       

        // ���信�� �ڹٽ�ũ��Ʈ���డ��
        mWebView.getSettings().setJavaScriptEnabled(true);
//        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        // Bridge �ν��Ͻ� ���
        mWebView.addJavascriptInterface(new AndroidBridge(), "HybridApp");
        mWebView.addJavascriptInterface(new AndroidBridge2(), "spinner");
        mWebView.loadUrl("file:///android_asset/test.html");  // ���� HTML ���� �ε�
//        mWebView.loadUrl("http://172.20.49.123:8080/Test/sw/index.html");
//        mWebView.loadUrl("http://172.20.49.24:9090/DevOnOffice/spinning/");
        mWebView.setWebChromeClient(new HelloWebChromeClient());
       mWebView.setWebViewClient(new HelloWebViewClient());  // WebViewClient ����    
       mButton.setOnClickListener( new OnClickListener(){
		public void onClick(View view) {
//			mWebView.loadUrl("javascript:setMessage('"+mEditText.getText()+"')");			
			mWebView.loadUrl("javascript:passok()");			
		
		}
       });
      
    }

    private class AndroidBridge {
        public void setMessage(final String arg) { // must be final
            handler.post(new Runnable() {
                public void run() {
                    Log.d("HybridApp", "setMessage("+arg+")");
                    mTextView.setText(mStringHead+arg);
                }
            });
        }
    }
    private class AndroidBridge2 {
        
    	public void checkpwd(final String arg) { // must be final
    		handler.post(new Runnable() {
		    	public void run() {
		    		Log.d("spinner", "setMessage("+arg+"2)");
		    		mTextView.setText(mStringHead+arg);
//		    		mWebView.loadUrl("javascript:passok()");
		    	}
    		});
    	}
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }
    
    private class HelloWebChromeClient extends WebChromeClient {
//        @Override
//        public boolean onJsBeforeUnload(WebView view, String url,
//                String message, JsResult result){
//            Log.e("JsBeforeUnload", ">> url :"+ url );
//            Log.e("JsBeforeUnload", ">> msg :"+ message);
//            return super.onJsBeforeUnload(view, url, message, result);
//        }
             @Override
            public boolean onJsAlert(WebView view, String url, String message,
                    JsResult result){
                     Log.e("Alert", ">> url :"+ url );
                     Log.e("Alert", ">> msg :"+ message);
                     result.confirm();
                     return false;
            }
             @Override
            public boolean onJsConfirm(WebView view, String url, String message,
                            JsResult result){
                         Log.e("JsConfirm", ">> url :"+ url );
                         Log.e("JsConfirm", ">> msg :"+ message);                
                        return super.onJsConfirm(view, url, message, result);
                    }
//         @Override
//        public boolean onJsPrompt(WebView view, String url, String message,
//                String defaultValue, JsPromptResult result){
//             Log.e("JsPrompt", ">> url :"+ url );
//             Log.e("JsPrompt", ">> msg :"+ message);
//            return super.onJsPrompt(view, url, message, defaultValue, result);
//        }
        
    }   
    
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}